/*
 * PanelDatosComunes.java
 *
 * Created on 28 de enero de 2008, 10:09
 */

package research.apneas.gui;

import java.beans.*;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;

import net.javahispano.jsignalwb.jsignalmonitor.TimeRepresentation;
import org.joda.time.DateTime;
import research.apneas.LimitacionFlujo;

/**
 *
 * @author  b
 */
public class PanelDatosComunes extends javax.swing.JPanel implements PropertyChangeListener {


    private LimitacionFlujo dim;
    private long principio, fin;
    private float duracion;
    private boolean usarEnEstadisticas;
    private byte posibilidad;
    private String evento;

    /** Creates new form PanelDatosComunes */
    public PanelDatosComunes(LimitacionFlujo dim) {
        initComponents();
        this.datePicker1.addPropertyChangeListener(this);
        this.datePicker2.addPropertyChangeListener(this);
        this.dim = dim;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        markTitleTextField = new javax.swing.JTextField();
        jTextFieldDate1 = new net.javahispano.jsignalwb.ui.JTextFieldDate();
        jTextFieldDate2 = new net.javahispano.jsignalwb.ui.JTextFieldDate();
        datePicker2 = new com.michaelbaranov.microba.calendar.DatePicker();
        datePicker1 = new com.michaelbaranov.microba.calendar.DatePicker();
        jLabel4 = new javax.swing.JLabel();
        textFieldDuration = new javax.swing.JTextField();
        textFieldPossibility = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jCheckBoxStatistics = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();

        jLabel1.setForeground(java.awt.Color.blue);
        jLabel1.setText("End time:");

        jLabel2.setForeground(java.awt.Color.blue);
        jLabel2.setText("Start time:");

        jLabel3.setForeground(java.awt.Color.blue);
        jLabel3.setText("Event:");

        markTitleTextField.setEditable(false);
        markTitleTextField.setForeground(java.awt.Color.blue);
        markTitleTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        markTitleTextField.setText("jTextField1");

        jTextFieldDate1.setForeground(java.awt.Color.blue);
        jTextFieldDate1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextFieldDate1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldDate1FocusLost(evt);
            }
        });

        jTextFieldDate2.setForeground(java.awt.Color.blue);
        jTextFieldDate2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextFieldDate2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldDate2FocusLost(evt);
            }
        });

        jLabel4.setForeground(java.awt.Color.blue);
        jLabel4.setText("Duration:");

        textFieldDuration.setEditable(false);
        textFieldDuration.setForeground(java.awt.Color.blue);
        textFieldDuration.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textFieldDuration.setText("jTextField1");

        textFieldPossibility.setEditable(false);
        textFieldPossibility.setForeground(java.awt.Color.blue);
        textFieldPossibility.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textFieldPossibility.setText("jTextField1");

        jLabel5.setForeground(java.awt.Color.blue);
        jLabel5.setText("Possibility:");

        jCheckBoxStatistics.setForeground(java.awt.Color.blue);

        jLabel6.setForeground(java.awt.Color.blue);
        jLabel6.setText("Use in statistics generation");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addContainerGap()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                          .addGap(43, 43, 43)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                              .addComponent(jLabel6)
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                              .addComponent(jCheckBoxStatistics))
                                    .addComponent(textFieldPossibility, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                                    .addComponent(textFieldDuration, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                                    .addComponent(jTextFieldDate2, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE, 139,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextFieldDate1, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                                    .addComponent(markTitleTextField, javax.swing.GroupLayout.Alignment.TRAILING,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(datePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(datePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, 22,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addContainerGap())
                );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextFieldDate1,
                        jTextFieldDate2, markTitleTextField, textFieldDuration, textFieldPossibility});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {datePicker1, datePicker2});

        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                              .addContainerGap()
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
                LEADING)
                .addComponent(jLabel3)
                .addComponent(markTitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
                LEADING)
                .addComponent(datePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jTextFieldDate1, javax.swing.GroupLayout.PREFERRED_SIZE,
                              javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.
                TRAILING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                          .addComponent(jLabel1)
                          .addComponent(jTextFieldDate2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(datePicker2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                              javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                              .addGap(37, 37, 37)
                                              .addComponent(jLabel2)))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(textFieldDuration, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(textFieldPossibility, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                  javax.swing.GroupLayout.DEFAULT_SIZE,
                                                  javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBoxStatistics)
                                    .addComponent(jLabel6))
                          .addContainerGap(101, Short.MAX_VALUE))
                );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {datePicker1, datePicker2});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jTextFieldDate1, jTextFieldDate2,
                        markTitleTextField, textFieldDuration, textFieldPossibility});

    } // </editor-fold>//GEN-END:initComponents

    private void jTextFieldDate1FocusLost(java.awt.event.FocusEvent evt) { //GEN-FIRST:event_jTextFieldDate1FocusLost
        updateDuration();
    } //GEN-LAST:event_jTextFieldDate1FocusLost

    private void jTextFieldDate2FocusLost(java.awt.event.FocusEvent evt) { //GEN-FIRST:event_jTextFieldDate2FocusLost
        updateDuration();
    } //GEN-LAST:event_jTextFieldDate2FocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.michaelbaranov.microba.calendar.DatePicker datePicker1;
    private com.michaelbaranov.microba.calendar.DatePicker datePicker2;
    private javax.swing.JCheckBox jCheckBoxStatistics;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private net.javahispano.jsignalwb.ui.JTextFieldDate jTextFieldDate1;
    private net.javahispano.jsignalwb.ui.JTextFieldDate jTextFieldDate2;
    private javax.swing.JTextField markTitleTextField;
    private javax.swing.JTextField textFieldDuration;
    private javax.swing.JTextField textFieldPossibility;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent evt) {
        if ("date".equals(evt.getPropertyName())) {
            if (evt.getSource().equals(datePicker1)) {
                Date date = datePicker1.getDate();
                if (date != null) {
                    jTextFieldDate1.setText(TimeRepresentation.timeToString(
                            swapDateNoChangeTime(
                                    dim.getPrincipioAbsoluto(), date.getTime())));
                }
            } else if (evt.getSource().equals(datePicker2)) {
                Date date = datePicker2.getDate();
                if (date != null) {
                    jTextFieldDate2.setText(TimeRepresentation.timeToString(
                            swapDateNoChangeTime(
                                    dim.getFinAbsoluto(), date.getTime())));
                }
            }
            updateDuration();
        }
    }

    private void updateDuration() {
        if (!jTextFieldDate2.getText().equals("__:__:__.___ || __/__/____") &&
            !jTextFieldDate1.getText().equals("__:__:__.___ || __/__/____")) {
            float d = (TimeRepresentation.stringToMillis(jTextFieldDate2.getText()) -
                       TimeRepresentation.stringToMillis(jTextFieldDate1.getText())) / 1000;
            this.textFieldDuration.setText("" + d);
            this.duracion = d;
        }
    }

    /**
     * @param old long
     * @param newTime long
     * @return long
     */
    private static long swapDateNoChangeTime(long old, long newTime) {
        DateTime dt = new DateTime(old);
        DateTime newDateTime = new DateTime(newTime).withTime(dt.getHourOfDay(),
                dt.getMinuteOfHour(), dt.getSecondOfMinute(),
                dt.getMillisOfSecond());

        return newDateTime.getMillis();
    }

    public long getPrincipio() {
        return datePicker1.getDate().getTime();
    }

    public void setPrincipio(long principio) {
        try {
            this.datePicker1.setDate(new Date(principio));
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
    }

    public long getFin() {
        return datePicker2.getDate().getTime();
    }

    public void setFin(long fin) {
        try {
            this.datePicker2.setDate(new Date(fin));
        } catch (PropertyVetoException ex) {
            ex.printStackTrace();
        }
    }

    public void setPosibilidad(byte posibilidad) {
        this.textFieldPossibility.setText("" + posibilidad);
    }

    public void setEvento(String evento) {
        this.markTitleTextField.setText("" + evento);
    }

    public float getDuracion() {
        return duracion;
    }

    public void setDuracion(float duracion) {
        this.duracion = duracion;
    }

    public boolean isUsarEnEstadisticas() {
        return this.jCheckBoxStatistics.isSelected();
    }

    public void setUsarEnEstadisticas(boolean usarEnEstadisticas) {
        this.jCheckBoxStatistics.setSelected(usarEnEstadisticas);
    }
}
